package org.dwpd.service.message.impl;

import lombok.extern.slf4j.Slf4j;
import org.dwpd.domain.common.Code;
import org.dwpd.domain.common.Result;
import org.dwpd.domain.user.UserDTO;
import org.dwpd.mapper.message.CommentMapper;
import org.dwpd.mapper.user.UserMapper;
import org.dwpd.domain.message.Comment;
import org.dwpd.domain.inbox.UserMessageRequest;
import org.dwpd.service.message.CommentService;
import org.dwpd.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.dwpd.domain.common.Code.RESULT_CODE_FAIL;

@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result addComment(Comment comment) {

        // 判断用户是否登录
        UserDTO user = UserHolder.getUser();
        if (user == null) {
            return Result.unlogin(-2, "用户未登录");
        }
        Integer userId = user.getUserId();
        // 判断用户是否在黑名单中
        Integer flag = userMapper.getUserFlagInBlack(userId);
        if (flag == 1) {
            return Result.fail(RESULT_CODE_FAIL, "用户已进入黑名单，无法进行评论");
        }

        comment.setCommentUserId(userId);
        log.info("添加新的评论：{}", comment);
        if (comment.getMessageId() == null || comment.getMessageId() <= 0
                || comment.getCommentUserId() == null || comment.getCommentUserId() <= 0
                || (comment.getRootCommentId() != null && comment.getRootCommentId() <= 0)
        ) {
            return Result.fail(Code.RESULT_CODE_FAIL, "请求参数错误");
        }
        commentMapper.insertComment(comment);

        // 封装返回结果
        UserMessageRequest userMessageRequest = responsePackaging(comment, userId);
        return Result.success(userMessageRequest);
    }

    @Override
    public Result getAllComment(Integer messageId) {
        log.info("获取用户帖id为: {} 的所有评论", messageId);
        if (messageId == null || messageId <= 0) {
            return Result.fail(Code.RESULT_CODE_FAIL, "请求参数错误");
        }
        List<Comment> result = commentMapper.selectAllComment(messageId);
        if (result == null || result.isEmpty()) {
            return Result.fail(Code.RESULT_CODE_FAIL, "获取数据失败");
        }
        return Result.success(result);
    }

    @Override
    public Result deleteComment(Integer commentId, Integer postUserId) {

        // 判断用户是否登录
        UserDTO user = UserHolder.getUser();
        if (user == null) {
            return Result.unlogin(-2, "用户未登录");
        }
        Integer userId = user.getUserId();
        log.info("删除评论:{}", commentId);
        if (commentId == null || commentId <= 0) {
            return Result.fail(Code.RESULT_CODE_FAIL, "请求参数错误");
        }
        // 非评论用户Id无法删除该评论
            Integer commentUserId = commentMapper.getCommentUserId(commentId);

        if (!commentUserId.equals(userId) && !postUserId.equals(userId)) {
            return Result.fail(Code.RESULT_CODE_FAIL, "权限不足，无法删除");
        }
        // 删除评论
        Integer total = commentMapper.deleteComment(commentId);
        if (total <= 0) {
            return Result.fail(Code.RESULT_CODE_FAIL, "删除评论失败");
        }

        return Result.success("删除评论成功");
    }

    private static UserMessageRequest responsePackaging(Comment comment, Integer userId) {
        UserMessageRequest userMessageRequest = new UserMessageRequest();
        userMessageRequest.setType("commentMessage");
        userMessageRequest.setMessageId(comment.getMessageId());
        userMessageRequest.setAcceptUserId(0);
        userMessageRequest.setUserId(userId);
        userMessageRequest.setContent("");
        userMessageRequest.setToken("");
        userMessageRequest.setIds("");
        return userMessageRequest;
    }
}
